import Tkinter
import time
import pylab
from threading import *

class ScreenSpace( Tkinter.Frame ):
  def __init__( self ):
      Tkinter.Frame.__init__( self, borderwidth=5, relief=Tkinter.GROOVE )
      self.master.title( "TinyInventor Application" )

      self.master.rowconfigure( 0, weight = 1 )
      self.master.columnconfigure( 0, weight = 1 )
      self.grid( sticky = Tkinter.W+Tkinter.E+Tkinter.N+Tkinter.S )
 
      self.s = Tkinter.Scrollbar( self, orient = Tkinter.VERTICAL )
      self.s.grid( row = 0, column = 2, rowspan = 1, sticky = Tkinter.W+Tkinter.E+Tkinter.N+Tkinter.S )
      self.text1 = Tkinter.Text( self, yscrollcommand=self.s.set, width = 55, height = 20 )
      self.text1.grid( row = 0, column = 0, rowspan = 1, columnspan = 2, sticky = Tkinter.W+Tkinter.E+Tkinter.N+Tkinter.S )
      self.text1.focus_set()
      self.s.config(command=self.text1.yview)
      #self.text1.insert( Tkinter.INSERT, "Printf Messages:\r\n" )

      self.rowconfigure( 1, weight = 1 )
      self.columnconfigure( 2, weight = 1 )

      self.bframe = Tkinter.Frame(self)
      self.bframe.grid( row = 0, column = 3, sticky = Tkinter.W+Tkinter.E+Tkinter.N+Tkinter.S )
      #self.bframe.rowconfigure( 0, weight = 1 )
      self.bframe.columnconfigure( 1, weight = 1 )

      self.eframe = Tkinter.Frame(self)
      self.eframe.grid( row = 1, column = 1, sticky = Tkinter.W+Tkinter.E+Tkinter.N+Tkinter.S )
      #self.eframe.rowconfigure( 0, weight = 1 )
      self.eframe.columnconfigure( 2, weight = 1 )

      self.addButton("Erase", self.eraseText)

  def printf( self, s ):
      self.text1.insert( Tkinter.END, s)
      self.text1.see( Tkinter.END )

  def eraseText( self ):
      self.text1.delete(1.0, Tkinter.END)

  curr_bframe_row=0
  def addButton( self, buttonText, buttonCommand ):
    b = Tkinter.Button( self.bframe, text = buttonText, height = 1, width = 15, command=buttonCommand )
    b.grid( row = self.curr_bframe_row, column = 0, columnspan = 1, sticky = Tkinter.W+Tkinter.E+Tkinter.N+Tkinter.S )
    b.pack()
    self.curr_bframe_row+=1

  curr_eframe_row=0
  def addEntry( self, label, number ):
    label = Tkinter.Label(self.eframe, text=label, width=20)
    entry = Tkinter.Entry(self.eframe, width=10)
    label.pack(side=Tkinter.LEFT)
    entry.pack(side=Tkinter.LEFT)
    entry.insert(0, number)
    label.grid( row = self.curr_eframe_row, column = 0, columnspan = 1, sticky = Tkinter.W+Tkinter.E+Tkinter.N+Tkinter.S )
    entry.grid( row = self.curr_eframe_row, column = 1, columnspan = 1, sticky = Tkinter.W+Tkinter.E+Tkinter.N+Tkinter.S )
    self.curr_eframe_row+=1
    return entry

    return self.entry.get().strip()

def safe_insert(_list, _idx, _val):
  while len(_list)<=_idx: _list.append(0)
  _list[_idx] = _val
